package com.project.weimi.utils;

import java.io.InputStream;

import android.content.Context;
import android.graphics.Bitmap;
import android.graphics.BitmapFactory;
import android.view.WindowManager;

public class BaseUtils {

	/**
	 * 判断Android系统API的版本
	 *
	 * @return
	 */
	public static int getAPIVersion() {
		int APIVersion;
		try {
			APIVersion = Integer.valueOf(android.os.Build.VERSION.SDK);
		} catch (NumberFormatException e) {
			APIVersion = 0;
		}
		return APIVersion;
	}

	/**
	 * 以最省内存的方式读取本地资源的图片
	 * 
	 * @param context
	 * @param resId
	 * @return Bitmap
	 */
	public static Bitmap readBitMap(Context context, int resId) {
		BitmapFactory.Options opt = new BitmapFactory.Options();
		opt.inPreferredConfig = Bitmap.Config.RGB_565;
		opt.inPurgeable = true;
		opt.inInputShareable = true;
		// 获取资源图片
		InputStream is = context.getResources().openRawResource(resId);
		return BitmapFactory.decodeStream(is, null, opt);
	}

	/**
	 * 以最省内存的方式读取本地资源的图片
	 * 
	 * @param context
	 * @param resId
	 * @return Bitmap
	 */
	public static Bitmap readBitMap(Context context, String pathName) {
		BitmapFactory.Options opt = new BitmapFactory.Options();
		opt.inPreferredConfig = Bitmap.Config.RGB_565;
		opt.inPurgeable = true;
		opt.inInputShareable = true;
		// 获取资源图片
		if (pathName.startsWith("file://")) {
			pathName = pathName.replace("file://", "");
		}
		return BitmapFactory.decodeFile(pathName, opt);
	}

	public static  DefaultDisplay getDefaultDisplay(Context context) {
		// TODO Auto-generated method stub
		DefaultDisplay defaultDisplay = new DefaultDisplay();
		WindowManager wm = (WindowManager) context.getSystemService(Context.WINDOW_SERVICE);

		defaultDisplay.width = wm.getDefaultDisplay().getWidth();
		defaultDisplay.height = wm.getDefaultDisplay().getHeight();
		
		return defaultDisplay;
	}

	public static class DefaultDisplay {
		public Integer width;
		public Integer height;

		public DefaultDisplay() {
			super();
			// TODO Auto-generated constructor stub
		}

		public DefaultDisplay(Integer width, Integer height) {
			super();
			this.width = width;
			this.height = height;
		}

		public Integer getWidth() {
			return width;
		}

		public void setWidth(Integer width) {
			this.width = width;
		}

		public Integer getHeight() {
			return height;
		}

		public void setHeight(Integer height) {
			this.height = height;
		}

		@Override
		public String toString() {
			return "DefaultDisplay [width=" + width + ", height=" + height + "]";
		}

	}
}
